package com.hxd.charitysystem.ui.activity

import android.view.View
import com.gyf.barlibrary.ImmersionBar
import com.hjq.dialog.WaitDialog
import com.hxd.charitysystem.R
import com.hxd.charitysystem.base.common.MyActivity
import com.hxd.charitysystem.bean.NetBaseBean
import com.hxd.charitysystem.bean.UserInfoBean
import com.hxd.charitysystem.handle.InputTextHelper
import com.hxd.charitysystem.utils.L
import com.hxd.charitysystem.utils.ProjectUrl
import com.hxd.tabactivityfragment.util.GsonToBean
import com.kymjs.rxvolley.RxVolley
import com.kymjs.rxvolley.client.HttpCallback
import com.kymjs.rxvolley.client.HttpParams
import com.vondear.rxtool.RxSPTool
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity


class LoginActivity : MyActivity() {

    private val loginWaitDialog by lazy { WaitDialog.Builder(this).setMessage("正在登录中...").show(); }
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun getTitleId(): Int {
        return R.id.tb_login_title
    }

    override fun initView() {
        InputTextHelper.Builder(this)
            .setMain(btn_login_commit)
            .addView(et_login_phone)
            .addView(et_login_password)
            .build()

        //点击登录按钮
        btn_login_commit.setOnClickListener {
            login()
        }
    }

    override fun initData() {

    }

    override fun statusBarConfig(): ImmersionBar {
        // 不要把整个布局顶上去
        return super.statusBarConfig().keyboardEnable(true)
    }

    override fun onRightClick(v: View?) {
        startActivity<RegisterActivity>()
    }

    /**
     * 登录逻辑
     */
    private fun login() {
        val account = et_login_phone.text.toString()
        val password = et_login_password.text.toString()

        val params = HttpParams()
        params.put("account",account)
        params.put("password",password)
        loginWaitDialog
        RxVolley.post(ProjectUrl.login, params, object:HttpCallback(){
            override fun onSuccess(result: String?) {
                val data = GsonToBean.JsonToBean<NetBaseBean<String>>(result)
                if(data.status == 200){
                    val token = data.msg

                    RxSPTool.putString(this@LoginActivity, "token", token)
//                    L.i("token : $token")
                    //下面这一部分网络请求为获取用户信息
                    //利用登录账号后的token值来获取
                    val par = HttpParams()
                    par.putHeaders("token", token)
                    ProjectUrl.getUserInfo(par, object:HttpCallback(){
                        override fun onSuccess(t: String?) {
//                            L.i("result : $t")
                            loginWaitDialog.dismiss()
                            val userInfo = GsonToBean.JsonToBean<UserInfoBean>(t)
                            if(userInfo.status == 200){
                                L.i("用户信息获取成功： data: $userInfo")
                                RxSPTool.putString(this@LoginActivity, "userInfo", userInfo.msg.userInfoToJson())
                                startActivity<MainActivity>()
                                finish()
                            }else{
                                L.i("用户信息获取失败： mag: ${userInfo.msg}")
                            }
                        }

                        override fun onFailure(errorNo: Int, strMsg: String?) {
                            loginWaitDialog.dismiss()
                            L.i("用户信息错误： errorNo:$errorNo, strMsg:$strMsg")
                        }
                    })
                }else{
                    toast(data.msg)
                }
            }

            override fun onFailure(errorNo: Int, strMsg: String?) {

            }
        })

    }

}
